home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap06 / Typer / Typer.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  9.5 KB  |  286 lines

  1. /*--------------------------------------
  2.    TYPER.C -- Typing Program
  3.               (c) Charles Petzold, 1998
  4.   --------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. #define BUFFER(x,y) *(pBuffer + y * cxBuffer + x)
  9.  
  10. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  11.  
  12. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  13.                     PSTR szCmdLine, int iCmdShow)
  14. {
  15.      static TCHAR szAppName[] = TEXT ("Typer") ;
  16.      HWND         hwnd ;
  17.      MSG          msg ;
  18.      WNDCLASS     wndclass ;
  19.      
  20.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  21.      wndclass.lpfnWndProc   = WndProc ;
  22.      wndclass.cbClsExtra    = 0 ;
  23.      wndclass.cbWndExtra    = 0 ;
  24.      wndclass.hInstance     = hInstance ;
  25.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  26.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  27.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  28.      wndclass.lpszMenuName  = NULL ;
  29.      wndclass.lpszClassName = szAppName ;
  30.      
  31.      if (!RegisterClass (&wndclass))
  32.      {
  33.           MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
  34.                       szAppName, MB_ICONERROR) ;
  35.           return 0 ;
  36.      }
  37.      
  38.      hwnd = CreateWindow (szAppName, TEXT ("Typing Program"),
  39.                           WS_OVERLAPPEDWINDOW,
  40.                           CW_USEDEFAULT, CW_USEDEFAULT,
  41.                           CW_USEDEFAULT, CW_USEDEFAULT,
  42.                           NULL, NULL, hInstance, NULL) ;
  43.      
  44.      ShowWindow (hwnd, iCmdShow) ;
  45.      UpdateWindow (hwnd) ;
  46.      
  47.      while (GetMessage (&msg, NULL, 0, 0))
  48.      {
  49.           TranslateMessage (&msg) ;
  50.           DispatchMessage (&msg) ;
  51.      }
  52.      return msg.wParam ;
  53. }
  54.  
  55. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  56. {
  57.      static DWORD   dwCharSet = DEFAULT_CHARSET ;
  58.      static int     cxChar, cyChar, cxClient, cyClient, cxBuffer, cyBuffer,
  59.                     xCaret, yCaret ;
  60.      static TCHAR * pBuffer = NULL ;
  61.      HDC            hdc ;
  62.      int            x, y, i ;
  63.      PAINTSTRUCT    ps ;
  64.      TEXTMETRIC     tm ;
  65.      
  66.      switch (message)
  67.      {
  68.      case WM_INPUTLANGCHANGE:
  69.           dwCharSet = wParam ;
  70.                                         // fall through
  71.      case WM_CREATE:
  72.           hdc = GetDC (hwnd) ;
  73.           SelectObject (hdc, CreateFont (0, 0, 0, 0, 0, 0, 0, 0,
  74.                                    dwCharSet, 0, 0, 0, FIXED_PITCH, NULL)) ;
  75.           
  76.           GetTextMetrics (hdc, &tm) ;
  77.           cxChar = tm.tmAveCharWidth ;
  78.           cyChar = tm.tmHeight ;
  79.           
  80.           DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
  81.           ReleaseDC (hwnd, hdc) ;
  82.                                         // fall through                
  83.      case WM_SIZE:
  84.                // obtain window size in pixels
  85.  
  86.           if (message == WM_SIZE)
  87.           {
  88.                cxClient = LOWORD (lParam) ;
  89.                cyClient = HIWORD (lParam) ;
  90.           }
  91.                // calculate window size in characters
  92.           
  93.           cxBuffer = max (1, cxClient / cxChar) ;
  94.           cyBuffer = max (1, cyClient / cyChar) ;
  95.           
  96.                // allocate memory for buffer and clear it
  97.           
  98.           if (pBuffer != NULL)
  99.                free (pBuffer) ;
  100.  
  101.           pBuffer = (TCHAR *) malloc (cxBuffer * cyBuffer * sizeof (TCHAR)) ;
  102.           
  103.           for (y = 0 ; y < cyBuffer ; y++)
  104.                for (x = 0 ; x < cxBuffer ; x++)
  105.                     BUFFER(x,y) = ' ' ;
  106.                     
  107.                // set caret to upper left corner
  108.  
  109.           xCaret = 0 ;
  110.           yCaret = 0 ;
  111.                     
  112.           if (hwnd == GetFocus ())
  113.                SetCaretPos (xCaret * cxChar, yCaret * cyChar) ;
  114.  
  115.           InvalidateRect (hwnd, NULL, TRUE) ;
  116.           return 0 ;
  117.                     
  118.      case WM_SETFOCUS:
  119.                // create and show the caret
  120.           
  121.           CreateCaret (hwnd, NULL, cxChar, cyChar) ;
  122.           SetCaretPos (xCaret * cxChar, yCaret * cyChar) ;
  123.           ShowCaret (hwnd) ;
  124.           return 0 ;
  125.           
  126.      case WM_KILLFOCUS:
  127.                // hide and destroy the caret
  128.  
  129.           HideCaret (hwnd) ;
  130.           DestroyCaret () ;
  131.           return 0 ;
  132.           
  133.      case WM_KEYDOWN:
  134.           switch (wParam)
  135.           {
  136.           case VK_HOME:
  137.                xCaret = 0 ;
  138.                break ;
  139.                
  140.           case VK_END:
  141.                xCaret = cxBuffer - 1 ;
  142.                break ;
  143.                
  144.           case VK_PRIOR:
  145.                yCaret = 0 ;
  146.                break ;
  147.                
  148.           case VK_NEXT:
  149.                yCaret = cyBuffer - 1 ;
  150.                break ;
  151.                
  152.           case VK_LEFT:
  153.                xCaret = max (xCaret - 1, 0) ;
  154.                break ;
  155.                
  156.           case VK_RIGHT:
  157.                xCaret = min (xCaret + 1, cxBuffer - 1) ;
  158.                break ;
  159.                
  160.           case VK_UP:
  161.                yCaret = max (yCaret - 1, 0) ;
  162.                break ;
  163.                
  164.           case VK_DOWN:
  165.                yCaret = min (yCaret + 1, cyBuffer - 1) ;
  166.                break ;
  167.                
  168.           case VK_DELETE:
  169.                for (x = xCaret ; x < cxBuffer - 1 ; x++)
  170.                     BUFFER (x, yCaret) = BUFFER (x + 1, yCaret) ;
  171.                
  172.                BUFFER (cxBuffer - 1, yCaret) = ' ' ;
  173.                
  174.                HideCaret (hwnd) ;
  175.                hdc = GetDC (hwnd) ;
  176.           
  177.                SelectObject (hdc, CreateFont (0, 0, 0, 0, 0, 0, 0, 0,
  178.                                    dwCharSet, 0, 0, 0, FIXED_PITCH, NULL)) ;
  179.                     
  180.                TextOut (hdc, xCaret * cxChar, yCaret * cyChar,
  181.                         & BUFFER (xCaret, yCaret),
  182.                         cxBuffer - xCaret) ;
  183.  
  184.                DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
  185.                ReleaseDC (hwnd, hdc) ;
  186.                ShowCaret (hwnd) ;
  187.                break ;
  188.           }
  189.           SetCaretPos (xCaret * cxChar, yCaret * cyChar) ;
  190.           return 0 ;
  191.           
  192.      case WM_CHAR:
  193.           for (i = 0 ; i < (int) LOWORD (lParam) ; i++)
  194.           {
  195.                switch (wParam)
  196.                {
  197.                case '\b':                    // backspace
  198.                     if (xCaret > 0)
  199.                     {
  200.                          xCaret-- ;
  201.                          SendMessage (hwnd, WM_KEYDOWN, VK_DELETE, 1) ;
  202.                     }
  203.                     break ;
  204.                     
  205.                case '\t':                    // tab
  206.                     do
  207.                     {
  208.                          SendMessage (hwnd, WM_CHAR, ' ', 1) ;
  209.                     }
  210.                     while (xCaret % 8 != 0) ;
  211.                     break ;
  212.                     
  213.                case '\n':                    // line feed
  214.                     if (++yCaret == cyBuffer)
  215.                          yCaret = 0 ;
  216.                     break ;
  217.                     
  218.                case '\r':                    // carriage return
  219.                     xCaret = 0 ;
  220.                     
  221.                     if (++yCaret == cyBuffer)
  222.                          yCaret = 0 ;
  223.                     break ;
  224.                     
  225.                case '\x1B':                  // escape
  226.                     for (y = 0 ; y < cyBuffer ; y++)
  227.                          for (x = 0 ; x < cxBuffer ; x++)
  228.                               BUFFER (x, y) = ' ' ;
  229.                          
  230.                     xCaret = 0 ;
  231.                     yCaret = 0 ;
  232.                          
  233.                     InvalidateRect (hwnd, NULL, FALSE) ;
  234.                     break ;
  235.                          
  236.                default:                      // character codes
  237.                     BUFFER (xCaret, yCaret) = (TCHAR) wParam ;
  238.                     
  239.                     HideCaret (hwnd) ;
  240.                     hdc = GetDC (hwnd) ;
  241.           
  242.                     SelectObject (hdc, CreateFont (0, 0, 0, 0, 0, 0, 0, 0,
  243.                                    dwCharSet, 0, 0, 0, FIXED_PITCH, NULL)) ;
  244.  
  245.                     TextOut (hdc, xCaret * cxChar, yCaret * cyChar,
  246.                              & BUFFER (xCaret, yCaret), 1) ;
  247.  
  248.                     DeleteObject (
  249.                          SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
  250.                     ReleaseDC (hwnd, hdc) ;
  251.                     ShowCaret (hwnd) ;
  252.  
  253.                     if (++xCaret == cxBuffer)
  254.                     {
  255.                          xCaret = 0 ;
  256.                          
  257.                          if (++yCaret == cyBuffer)
  258.                               yCaret = 0 ;
  259.                     }
  260.                     break ;
  261.                }
  262.           }
  263.           
  264.           SetCaretPos (xCaret * cxChar, yCaret * cyChar) ;
  265.           return 0 ;
  266.           
  267.      case WM_PAINT:
  268.           hdc = BeginPaint (hwnd, &ps) ;
  269.           
  270.           SelectObject (hdc, CreateFont (0, 0, 0, 0, 0, 0, 0, 0,
  271.                                    dwCharSet, 0, 0, 0, FIXED_PITCH, NULL)) ;
  272.                               
  273.           for (y = 0 ; y < cyBuffer ; y++)
  274.                TextOut (hdc, 0, y * cyChar, & BUFFER(0,y), cxBuffer) ;
  275.  
  276.           DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
  277.           EndPaint (hwnd, &ps) ;
  278.           return 0 ;
  279.           
  280.      case WM_DESTROY:
  281.           PostQuitMessage (0) ;
  282.           return 0 ;
  283.      }
  284.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  285. }
  286.